package cn.silver.module.authority.service.impl;

import cn.silver.framework.mybatis.service.impl.BusServiceImpl;
import cn.silver.framework.satoken.enums.DeviceTypeEnums;
import cn.silver.module.authority.domain.SysClient;
import cn.silver.module.authority.mapper.SysClientMapper;
import cn.silver.module.authority.bean.ClientBean;
import cn.silver.module.authority.param.ClientParam;
import cn.silver.module.authority.service.ISysClientService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 客户端管理Service业务层处理
 *
 * @author Silver Zhou
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysClientServiceImpl extends BusServiceImpl<SysClientMapper, SysClient, ClientParam, ClientBean> implements ISysClientService {

    @Override
    public List<String> init() {
        List<DeviceTypeEnums> devices = Arrays.stream(DeviceTypeEnums.values()).filter(DeviceTypeEnums::getBuilt).toList();
        if (CollectionUtils.isEmpty(devices)) {
            List<String> codes = devices.stream().map(DeviceTypeEnums::getCode).distinct().toList();
            List<ClientBean> clinets = this.baseMapper.selectVoList(new LambdaQueryWrapper<SysClient>().eq(SysClient::getStatus, "00")
                    .in(SysClient::getDeviceType, codes));
            List<String> exists = clinets.stream().map(ClientBean::getDeviceType).distinct().toList();
            devices.stream().filter(item -> !exists.contains(item.getCode())).forEach(item -> this.baseMapper.insert(SysClient.getInstant(item)));
        }
        return devices.stream().map(DeviceTypeEnums::getName).distinct().toList();
    }
}
